package za.org.jcicada.cfgmngr.impl;

import java.util.List;
import java.util.Set;

import za.org.jcicada.cfgmngr.IPhysicalClient;
import za.org.jcicada.cfgmngr.IPhysicalServer;
import za.org.jcicada.cfgmngr.messages.AttachReply;
import za.org.jcicada.cfgmngr.messages.DetachReply;
import za.org.jcicada.cfgmngr.messages.GetChildrenReply;
import za.org.jcicada.cfgmngr.messages.NodeSystemsReply;
import za.org.jcicada.cfgmngr.messages.ViewEventInform;
import za.org.jcicada.cfgmngr.messages.ViewSensorInform;
import za.org.jcicada.cfgmngr.messages.ViewSensorsReply;
import za.org.jcicada.comms.IConnection;
import za.org.jcicada.comms.impl.MessagingBase;
import za.org.jcicada.genss.PhysicalURN;
import za.org.jcicada.genss.SystemURN;
import za.org.jcicada.genss.messages.Result;
import za.org.jcicada.genss.sensor.SensorURN;

public class PhysicalMessaging extends MessagingBase {

	/**
	 * Handle to physical server for invoking methods on.
	 */
	IPhysicalServer physicalServer;
	
	/**
	 * Handle to callback object for messages from the server to the client.
	 */
	IPhysicalClient client;
	
	public PhysicalMessaging(IConnection connection, IPhysicalServer physicalServer) {
		super(connection);
		this.physicalServer = physicalServer;
		client = new Responder();
	}
	
	public void onClose() throws Exception {
		physicalServer.informDisconnect(client);

	}
	
	public IPhysicalClient getClient() {
		return client;
	}
	
	public void cfgmngrGetChildrenRequest(za.org.jcicada.cfgmngr.messages.GetChildrenRequest msg) {
		physicalServer.requestGetChildren(client, msg.getParentURN());
	}
	
	public void cfgmngrViewSensorsRequest(za.org.jcicada.cfgmngr.messages.ViewSensorsRequest msg) {
		physicalServer.requestViewSensors(client, msg.getView(), msg.getPhysicalURNs());
	}
	
	public void cfgmngrNodeSystemsRequest(za.org.jcicada.cfgmngr.messages.NodeSystemsRequest msg) {
		physicalServer.requestNodeSystems(client, msg.getPhysicalURN());
	}
	
	public void cfgmngrAttachRequest(za.org.jcicada.cfgmngr.messages.AttachRequest msg) {
		physicalServer.requestAttach(client);
	}
	
	public void cfgmngrDetachRequest(za.org.jcicada.cfgmngr.messages.DetachRequest msg) {
		physicalServer.requestDetach(client);
	}
	
	/**
	 * Implementation of IPhysicalClient that sends messages back to a
	 * comms connection.
	 */
	public class Responder extends MessagingBase.ResponderBase implements IPhysicalClient {

		public void replyGetChildren(Result result, PhysicalURN parentURN, List<PhysicalURN> childrenURNs) {
			sendMessage(new GetChildrenReply(result, parentURN, childrenURNs));
		}
		
		public void replyViewSensors(Result result, String view, int number) {
			sendMessage(new ViewSensorsReply(result, view, number));
		}

		public void informViewSensor(String view, PhysicalURN physicalURN, SensorURN sensorURN) {
			sendMessage(new ViewSensorInform(view, physicalURN, sensorURN));
		}
		
		public void replyNodeSystems(Result result, PhysicalURN physicalURN, Set<SystemURN> systemURNs) {
			sendMessage(new NodeSystemsReply(result, physicalURN, systemURNs));
		}
		
		public void replyAttach(Result result) {
			sendMessage(new AttachReply(result));
		}
		
		public void replyDetach(Result result) {
			sendMessage(new DetachReply(result));
		}
		
		public void informViewEvent(String view, Event event) {
			sendMessage(new ViewEventInform(view, event));
		}

	}

}
